﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Leadtools.WinForms;
using MINERP.MODEL.NewPinBan;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Design;
using Leadtools;
using Leadtools.ImageProcessing;

namespace MINERP.NewPB.Presenter
{

    public delegate void MyPicViewMoveHandler(PicView sender, Point newPoint);
    //public delegate void WhirlHandler(PicView sender);
    /// <summary>
    /// 用于存放图片以及相关属性的图片矩形类
    /// </summary>
    public class PicView : RasterImageViewer
    {
        public event MyPicViewMoveHandler PicMoveEvent;
        //public event WhirlHandler PicWhirlEvent;

        public bool LeftAnchor { get; set; }//左边锚上
        public bool RightAnchor { get; set; }//右边锚上
        public bool UpAnchor { get; set; }//上边锚上
        public bool DownAnchor { get; set; }//下边锚上

        public Rectangle Rect// 此图形的矩形对像
        {
            get
            {
                return new Rectangle(x, y, Width, Height);
            }
        }

        public bool IsOutRedLine//是否在红线外
        {
            get
            {
                if (Left >= DataTransfer.inchBoardWidth)
                {
                    return true;
                }
                //else if (Right > DataTransfer.inchBoardWidth)
                //{
                //    Location = new Point(DataTransfer.inchBoardWidth + 10, (Location.Y < 0) || (Location.Y+Height > DataTransfer.inchBoardHeight) ? 10 : Location.Y);
                //    return true;
                //}
                else
                {
                    return false;
                }
            }
        }

        public bool IsOverRedLin//是否压线红
        {
            get
            {
                if (Left <= DataTransfer.inchBoardWidth && Right > DataTransfer.inchBoardWidth)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public Label _lblPName;//用来显示照片的名称
        public Label _lblPSize;//用来显示照片的尺寸
        public int Area { get { return Width * Height; } }//“面积”指在屏幕上以相素点为单位的面积
        public PhotoInfo_PinBan PInfo { get; set; }
        public int x { get { return base.Location.X; } }//左上角的X坐标
        public int y { get { return base.Location.Y; } }//左上角的Y坐标

        private bool _isWhril = false;
        public bool IsWhirl { get { return _isWhril; } set { _isWhril = value; PInfo.IsWhirl = value; WhirlPic(); } }//是否旋转,默认为false

        public string PicName { get { return "pic" + PInfo.PhotoId.ToString(); } }

        public new Point Location { get { return base.Location; } set { base.Location = value; PInfo.Location = value; } }
        public Point Point1 { get { return new Point(x, y); } }//左上
        public Point Point2 { get { return new Point(x + base.Width, y); } }//右上
        public Point Point3 { get { return new Point(x + base.Width, y + base.Height); } }//右下
        public Point Point4 { get { return new Point(x, y + base.Height); } }//左下

        public int Type { get; set; }//0：默认的为正常的图片，1：为多选的图片集合,2：代表被框选的国片

        /// <summary>
        /// 宽，单位：屏幕上的像素
        /// </summary>
        public new int Width
        {
            get { return base.Width; }
            set
            {
                base.Width = value;
            }
        }
        /// <summary>
        /// 高，单位：屏幕上的像素
        /// </summary>
        public new int Height
        {
            get { return base.Height; }
            set
            {
                base.Height = value;
            }
        }


        private const int SPI_GETDRAGFULLWINDOWS = 38;
        private const int SPI_SETDRAGFULLWINDOWS = 37;
        private const int SPIF_SENDWININICHANGE = 2;
        private const int SPIF_UPDATEINIFILE = 1;

        public const int WM_MOVING = 0x0216;
        public const int WM_MOVE = 0x0003;
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_MOVING:
                    {
                        DevExpress.Utils.Drawing.Helpers.NativeMethods.RECT rect = (DevExpress.Utils.Drawing.Helpers.NativeMethods.RECT)Marshal.PtrToStructure(m.LParam, typeof(DevExpress.Utils.Drawing.Helpers.NativeMethods.RECT));
                        if (PicMoveEvent != null)
                        {
                            this.Cursor = Cursors.Hand;
                            PicMoveEvent(this, rect.ToRectangle().Location);
                        }

                        break;
                    }
                case WM_MOVE:
                    {
                        this.Cursor = Cursors.Default;
                        break;
                    }
            }
            base.WndProc(ref m);
        }

        RasterImage _curImg = null;//保存当前的图片

        public PicView(PhotoInfo_PinBan pinfo, Point locatPoint)
        {
            this.Paint += new PaintEventHandler(PicView_Paint);
            this.MouseEnter += new EventHandler(PicView_Enter);
            this.MouseLeave += new EventHandler(PicView_Leave);

            PInfo = pinfo;
            PInfo.BarCodeStatus = 2;//黙认无条码
            Location = locatPoint;
            Width = Convert.ToInt32(PInfo.Width * DataTransfer.perPix);
            Height = Convert.ToInt32(PInfo.Height * DataTransfer.perPix);

            //if (DataTransfer.curTemplate.HasBarCode)
            //{
            PInfo.BarCodeStatus = DataTransfer.BarCodeStatus;
            if (DataTransfer.BarCodeStatus == 1)
            {
                Width += DataTransfer.BarCodePixWidth;
            }
            else if (DataTransfer.BarCodeStatus == 0)
            {
                Height += DataTransfer.BarCodePixWidth;
            }

            //}

            _isWhril = pinfo.IsWhirl;
            if (_isWhril)
            {
                int newWidth, newHeight;
                if (DataTransfer.BarCodeStatus == 1)//有条码旋转，并且条码在左右
                {
                    newWidth = Height + DataTransfer.BarCodePixWidth;
                    newHeight = Width - DataTransfer.BarCodePixWidth;
                }
                else// 有条码旋转,并表条码在上下
                {
                    newWidth = Height - DataTransfer.BarCodePixWidth;
                    newHeight = Width + DataTransfer.BarCodePixWidth;
                }

                Width = newWidth;
                Height = newHeight;
            }


            base.AutoScroll = false;
            base.BorderStyle = System.Windows.Forms.BorderStyle.None;
            base.FrameColor = Color.YellowGreen;
            base.SizeMode = Leadtools.RasterPaintSizeMode.Stretch;

            //照片ID
            _lblPName = new Label();
            _lblPName.Text = "Pic" + PInfo.PhotoId.ToString();
            _lblPName.Location = new Point(1, 1);
            _lblPName.AutoSize = true;
            _lblPName.BackColor = Color.Transparent;
            _lblPName.ForeColor = Color.Red;
            _lblPName.Font = new System.Drawing.Font("宋体", 6.75F, System.Drawing.FontStyle.Regular);
            _lblPName.BringToFront();
            base.Controls.Add(_lblPName);

            //照片尺寸
            _lblPSize = new Label();
            _lblPSize.Text = PInfo.PhotoSize + "|" + PInfo.PhotoSiziCM;
            _lblPSize.AutoSize = true;
            _lblPSize.BackColor = Color.Transparent;
            _lblPSize.ForeColor = Color.Red;
            _lblPSize.Font = new System.Drawing.Font("宋体", 6.75F, System.Drawing.FontStyle.Regular);
            _lblPSize.BringToFront();
            _lblPSize.Location = new Point(1, 10);
            base.Controls.Add(_lblPSize);

            IilitialImg();
        }

        public void IilitialImg()
        {
            if (DataTransfer.IsShowImg)
            {
                try
                {
                    if (_curImg == null)
                    {
                        _curImg = CommonUtil.GetStampPic(PInfo.PicPath);
                    }

                    if (_curImg != null)
                    {
                        RotatPic(_curImg);
                    }

                    base.Image = _curImg;
                }
                catch (Exception)
                {
                    System.Windows.Forms.MessageBox.Show("没有找到图片，请检查图片是否被移走" + PInfo.PicPath);
                }
            }
            else
            {
                if (base.Image != null)
                {
                    _curImg = base.Image.CloneAll();
                }

                base.Image = null;
            }
        }

        private void WhirlPic()//旋转图片
        {
            if (DataTransfer.IsShowImg)
            {
                if (_curImg != null)
                {
                    RotatPic(_curImg);
                    base.Image = _curImg;
                }
            }
        }

        private bool isWhrilIng = false;
        private void RotatPic(RasterImage img)
        {
            RotateCommand command = new RotateCommand();
            command.Flags = RotateCommandFlags.Resize;
            if (_isWhril)
            {
                //command.Angle = -90 * 100;
                if (!isWhrilIng)
                {
                    command.Angle = -90 * 100;
                    command.Run(img);
                    isWhrilIng = true;
                }
            }
            else
            {
                if (isWhrilIng)
                {
                    command.Angle = 90 * 100;
                    command.Run(img);
                    isWhrilIng = false;
                } 
                //command.Angle = 90 * 100;
            }
            //command.Run(img);
        }

        public PicView(int photoid, int width, int height, Point locatPoint)//用于构建零时的图片，主要用于虚线框图片（包括移动、或是旋转）用来和其它图片验证是否重叠等用途
        {
            this.Paint += new PaintEventHandler(PicView_Paint);
            base.AutoScroll = false;
            this.MouseEnter += new EventHandler(PicView_Enter);
            this.MouseLeave += new EventHandler(PicView_Leave);

            PInfo = new PhotoInfo_PinBan();
            PInfo.PhotoId = photoid;
            Width = width;
            Height = height;
            Location = locatPoint;
        }

        Color BoardColor = Color.Black;

        Point pStart;
        Point pEnd;
        void PicView_Paint(object sender, PaintEventArgs e)
        {
            ButtonBorderStyle bs = ButtonBorderStyle.Solid;
            if (Type != 0)
            {
                bs = ButtonBorderStyle.Dotted;
                BoardColor = Color.Red;
            }

            ControlPaint.DrawBorder(e.Graphics,
                        base.ClientRectangle,
                        BoardColor,
                        1,
                        bs,
                        BoardColor,
                        1,
                        bs,
                        BoardColor,
                        1,
                        bs,
                        BoardColor,
                        1,
                        bs);
            if (Type != 1)
            {
                switch (PInfo.BarCodeStatus)
                {
                    case 1:
                        pStart = new Point(2, 5);
                        pEnd = new Point(2, this.Height - 5);
                        DrawALine(e.Graphics, Color.Tomato, pStart, pEnd);
                        break;
                    case 0:
                        pStart = new Point(5, 2);
                        pEnd = new Point(this.Width - 5, 2);
                        DrawALine(e.Graphics, Color.Tomato, pStart, pEnd);
                        break;
                    case 2:
                        break;
                    default:
                        break;
                }
            }
        }

        public void DrawALine(Graphics g, Color color, Point pointStart, Point pointEnd)
        {
            Pen p = new Pen(color, 2);
            g.DrawLine(p, pointStart, pointEnd);
        }


        /// <summary>
        /// 框选多图专用
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="locatPoint"></param>
        /// <param name="type"></param>
        public PicView(int width, int height, Point locatPoint)//框选多图专用
        {
            this.Paint += new PaintEventHandler(PicView_Paint);
            base.AutoScroll = false;
            this.MouseEnter += new EventHandler(PicView_Enter);
            this.MouseLeave += new EventHandler(PicView_Leave);

            PInfo = new PhotoInfo_PinBan();
            PInfo.PhotoId = 0;
            Width = width;
            Height = height;
            Location = locatPoint;
            base.BorderStyle = BorderStyle.FixedSingle;
            this.Type = 1;//代表多框选

        }

        private void GetSuitPoint(PicView pv)
        {
            Point p = pv.Location;
            if (p.X < 0)
            {
                p.X = 0;
            }
            if (pv.Location.Y < 0)
            {
                p.Y = 0;
            }
            if (p.Y + pv.Height > DataTransfer.inchBoardHeight)
            {
                p.Y = DataTransfer.inchBoardHeight - pv.Height;
            }
            if (Right > DataTransfer.inchBoardWidth && Left <= DataTransfer.inchBoardWidth)//判断是否压红线
            {
                p.X = DataTransfer.inchBoardWidth + 10;
                //p.Y = (p.Y < 0)||(p.Y>DataTransfer.inchBoardHeight) ? 10 : p.Y;
            }

            pv.Location = p;
        }

        void PicView_Leave(object sender, EventArgs e)
        {
            BoardColor = Color.Black;
            //((RasterImageViewer)sender).BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        }

        void PicView_Enter(object sender, EventArgs e)
        {
            BoardColor = Color.Red;
            //((RasterImageViewer)sender).BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }
    }
}
